package sz

import (
	"common/ut"
	"sort"
	"sync"
)

type Logic struct {
	sync.RWMutex
	cards []int // 52张牌
}

func NewLogic() *Logic {
	return &Logic{}
}

// 洗牌 顺序： 方片、梅花、红桃、黑桃
func (l *Logic) washCards() {
	if l.cards == nil {
		return
	}
	// 16 进制，每种花色 13 张牌
	l.cards = []int{
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
		0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
		0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
		0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d,
	}
	// Fisher-Yates 算法
	for i := len(l.cards) - 1; i > 0; i-- {
		j := ut.Rand(i + 1)
		l.cards[i], l.cards[j] = l.cards[j], l.cards[i]
	}
}

// getCards 获取三张手牌
func (l *Logic) getCards() []int {
	cards := make([]int, 3)
	l.Lock()
	defer l.Unlock()
	for i := 0; i < 3; i++ {
		if len(cards) == 0 {
			break
		}
		// 获取并移除最后一张牌
		card := l.cards[len(l.cards)-1]
		l.cards = l.cards[:len(l.cards)-1]
		cards[i] = card
	}
	return cards
}

// CompareCards 比牌操作 和：0 赢：>0 失败：<0
func (l *Logic) CompareCards(from []int, to []int) int {
	// 1. 获取牌类型
	fromType := l.getCardsType(from)
	ToType := l.getCardsType(to)
	// 2. 先比较类型
	if fromType != ToType {
		return int(fromType - ToType)
	}
	// 3. 对子两张相等，先处理
	if fromType == DuiZi {
		duiFrom, danFrom := l.getDuiZi(from)
		duiTo, danTo := l.getDuiZi(to)
		// 3.1. 先比较对子
		if duiFrom != duiTo {
			return duiFrom - duiTo
		}
		// 3.2. 再比较单张
		return danFrom - danTo
	}
	// 4. 比较牌面
	valuesFrom := l.getCardValues(from)
	valuesTo := l.getCardValues(to)
	if valuesFrom[2] != valuesTo[2] {
		return valuesFrom[2] - valuesTo[2]
	} else if valuesFrom[1] != valuesTo[1] {
		return valuesFrom[1] - valuesTo[1]
	}
	return valuesFrom[0] - valuesTo[0]
}

func (l *Logic) getCardsType(cards []int) CardsType {
	one := l.getCardNumber(cards[0])
	two := l.getCardNumber(cards[1])
	three := l.getCardNumber(cards[2])
	// 1. 豹子：三张牌相同
	if one == two && two == three {
		return BaoZi
	}
	// 2. 金花：颜色相同
	jinhua := false
	oneColor := l.getCardColor(cards[0])
	twoColor := l.getCardColor(cards[1])
	threeColor := l.getCardColor(cards[2])
	if oneColor == twoColor && twoColor == threeColor {
		jinhua = true
	}
	// 3. 顺子
	shunzi := false
	values := l.getCardValues(cards)
	oneV := values[0]
	twoV := values[1]
	threeV := values[2]
	if oneV+1 == twoV && two+1 == threeV {
		shunzi = true
	} else if oneV == 2 && twoV == 3 && threeV == 14 { // 14 是 A
		shunzi = true
	}
	if jinhua && shunzi {
		return ShunJin
	} else if jinhua {
		return JinHua
	} else if shunzi {
		return ShunZi
	} else if oneV == twoV || oneV == threeV || twoV == threeV {
		return DuiZi
	}
	return DanZhang
}

// 获取牌面值
func (l *Logic) getCardNumber(card int) int {
	return card & 0x0f
}

// 获取排面颜色
func (l *Logic) getCardColor(card int) CardColor {
	return Colors[card/0x10]
}

func (l *Logic) getCardValue(card int) int {
	value := l.getCardNumber(card)
	// A 是最大的
	if value == 1 {
		value += 13
	}
	return value
}

// 获取牌的实际值
func (l *Logic) getCardValues(cards []int) []int {
	values := make([]int, len(cards))
	for i, card := range cards {
		values[i] = l.getCardValue(card)
	}
	sort.Ints(values)
	return values
}

// 获取对子
func (l *Logic) getDuiZi(cards []int) (int, int) {
	values := l.getCardValues(cards)
	oneV := values[0]
	twoV := values[1]
	threeV := values[2]
	if oneV == twoV { // AAB
		return oneV, threeV
	} else if oneV == threeV { // ABA
		return threeV, twoV
	}
	return twoV, threeV // ABB
}
